package com.gxa.shop.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * com.gxa.filter.CORSFilter
 * User: huangly
 * Date: 2024/12/02 16:17
 * motto:   逆水行舟不进则退
 * Description:
 * Version: v1.0
 */
/**
 * 设置跨域请求 可接收 跨域的请求
 */
@WebFilter(filterName = "CORSFilter",urlPatterns = "/*")
public class CORSFilter implements Filter {
    private static final String OPTIONS = "OPTIONS";
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("com.gxa.filter.CORSFilter -- 系统启动...");
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        // 设置字符集
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        // 处理跨域请求
        String origin = request.getHeader("Origin");
        if ("http://localhost:5173".equals(origin)) { // 根据实际情况调整允许的来源
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Credentials", "true");
        } else {
            response.setHeader("Access-Control-Allow-Origin", "*");
            // 注意：如果你不允许凭证共享，则不需要设置 Access-Control-Allow-Credentials
        }

        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "*");

        if (OPTIONS.equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK); // 预检请求返回200状态码
            return;
        }

        filterChain.doFilter(req, res);
//        HttpServletRequest request = (HttpServletRequest) req;
//        request.setCharacterEncoding("utf-8");
//        System.out.println("com.gxa.filter.CORSFilter -- 过滤器放行前...." + request.getRequestURL());
//        HttpServletResponse response = (HttpServletResponse) res;
//        response.setCharacterEncoding("utf-8");//设置字符集
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
//        response.setHeader("Access-Control-Max-Age", "3600");
//        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,  Content-Type, 'Authorization'");
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        if(OPTIONS.equalsIgnoreCase(request.getMethod()))
//            return;
//        filterChain.doFilter(req, res);
//        System.out.println("com.gxa.filter.CORSFilter -- 过滤器返回后...." + request.getRequestURL());
    }

    public void destroy() {
        System.out.println("com.gxa.filter.CORSFilter -- 系统关闭...");
    }
}

